//
//  YTBaseEditorStringView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/22.
//

#import "YTBaseEditorStringView.h"


@interface YTBaseEditorStringView ()<UITextFieldDelegate>
@property (nonatomic ,strong) UILabel * forceFlagLab;
@property (nonatomic ,strong) UILabel * nameLab;
@property (nonatomic ,strong) UITextField * contentLab;
@property (nonatomic ,strong) UIView * makeview;

@end

@implementation YTBaseEditorStringView
+ (instancetype)NewViewWithNil{
    YTBaseEditorStringView * view = [YTBaseEditorStringView NewView];
    [DTKTool YJ:0 andView:view.makeview];
    [view.makeview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.bottom.offset(0);
    }];
    return view;
}
+ (instancetype)NewViewWithUP{
    YTBaseEditorStringView * view = [YTBaseEditorStringView NewView];
    [view.makeview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(0);
        make.bottom.offset(kwt(-10));
    }];
    return view;
}
+ (instancetype)NewView{
    YTBaseEditorStringView * view = [YTBaseEditorStringView new];
    view.backgroundColor = UIColor.whiteColor;
    view.clipsToBounds = YES;
    [view setupview];
    return view;
}
- (void)setMakeColor:(UIColor *)makeColor{
    self.makeview.backgroundColor = makeColor;
}
- (void)setupview{
    
    UIView * makeview = [UIView new];
    self.makeview = makeview;
    makeview.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:makeview];
    [self addSubview:makeview];
    [makeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(kwt(5));
        make.bottom.offset(kwt(-5));
    }];
    
    UILabel * forceFlagLab = [DTKTool labelWithText:@"*" andFont:kCFont(17) andTextColor:KcolorHex(@"#FA0F0F")];
    self.forceFlagLab = forceFlagLab;
    forceFlagLab.mas_KLS().mas_KYS();
    [makeview addSubview:forceFlagLab];
    [forceFlagLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * nameLab = [DTKTool labelWithText:@"--" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    self.nameLab = nameLab;
    nameLab.mas_KLS().mas_KYS();
    [makeview addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(forceFlagLab.mas_right);
        make.centerY.offset(0);
    }];
    
    UITextField * contentLab = [UITextField new];
    contentLab.delegate = self;
    contentLab.placeholder = @"请填写";
    contentLab.attributedPlaceholder = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"请填写"] andNodeColor:KcolorHex(@"#787A79") andSeledColor:KcolorHex(@"#787A79") andFont:kFont(18) andPattenr:@""];
    contentLab.font = kFont(18);
    self.contentLab = contentLab;
    contentLab.textColor = KcolorHex(@"#222222");
    contentLab.textAlignment = NSTextAlignmentRight;
    [makeview addSubview:contentLab];
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLab.mas_right).offset(kwt(10));
        make.right.offset(kwt(-10));
        make.centerY.offset(0);
    }];
    
    
}
- (void)setPlaceholder:(NSString *)placeholder{
    self.contentLab.placeholder = placeholder;
}
- (void)setKeyboardType:(UIKeyboardType)keyboardType{
    self.contentLab.keyboardType = keyboardType;
}
- (void)viewclick{
    if(self.block){
        self.block(self.content);
    }
}
- (void)setForceFlag:(BOOL)forceFlag {
    _forceFlag = forceFlag;
    if(forceFlag){
        self.forceFlagLab.text = @"*";
    } else {
        self.forceFlagLab.text = @"";
    }
}

- (void)setName:(NSString *)name{
    _name = name;
    self.nameLab.text = name;
//    self.contentLab.attributedPlaceholder = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"请填写"] andNodeColor:KcolorHex(@"#787A79") andSeledColor:KcolorHex(@"#787A79") andFont:kFont(18) andPattenr:@""];
}
-(void)setContent:(NSString *)content{
    _content = content;
    self.contentLab.text = content;

}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if(self.block){
        self.block(textField.text);
    }
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if((textField.keyboardType == UIKeyboardTypeNumberPad) ){
        // 将光标移动到文本的末尾
        if ([textField isFirstResponder]) {
//            UITextPosition *oldPosition = [textField positionFromPosition:textField.beginningOfDocument offset:0];
//            UITextPosition *newPosition = [textField positionFromPosition:textField.endOfDocument offset:0];
//            UITextRange *newRange = [textField textRangeFromPosition:oldPosition toPosition:newPosition];
//            textField.selectedTextRange = newRange;
            if([textField.text isEqualToString:@"0"]){
                textField.text = @"";
            }
        }
    }
  
}
@end
